﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.collect
{
    public partial class cn_list : He.Web.UI.Manage
    {
        public int rcount;
        public int psize;
        public int page;
        public string keywords;
        public string channel;
        ///http://sou.chinanews.com/search.do?q=%E7%BD%97%E6%B0%B8%E6%B5%A9&ps=10&time_scope=0&channel=all&sort=score&adv=1&day1=&day2=&field=&direction=desc&pager=0
        ///http://sou.chinanews.com/search.do?q=%E7%BD%97%E6%B0%B8%E6%B5%A9&ps=10&channel=all&sort=score&pager=0

        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            keywords = GetString("keywords");
            psize = GetInt("psize", 10);
            channel = GetString("channel");

            if (!this.Page.IsPostBack)
            {
                chkLoginLevel("collect");
                ddlCategoryBind();
                RptBind();
            }
        }

        #region 类别绑定
        private void ddlCategoryBind()
        {
            BLL.channel sbll = new BLL.channel();
            BLL.category cbll = new BLL.category();
            //绑定中文类别
            DataTable he_sys = sbll.GetList("is_lock=0 and model_id=" + (int)Enums.sys_model.文章模块).Tables[0];
            foreach (DataRow dr_sys in he_sys.Rows)
            {
                string lang_ver = LangHelper.GetLangVer(dr_sys["lang"]);
                DataTable he_category = cbll.GetList(0, Convert.ToInt32(dr_sys["id"]));
                foreach (DataRow dr_category in he_category.Rows)
                {
                    string Id = dr_category["id"].ToString();
                    int layer = int.Parse(dr_category["layer"].ToString());
                    string Title = dr_category["title"].ToString().Trim();

                    if (layer == 1)
                    {
                        Title = string.Format("【{0}->{1}】{2}", lang_ver, dr_sys["manage_title"], Title);
                        ddlCategoryId.Items.Add(new ListItem(Title, Id));
                    }
                    else
                    {
                        Title = "├ " + Title;
                        Title = StringHelper.Repeat("　", layer - 1) + Title;
                        ddlCategoryId.Items.Add(new ListItem(Title, Id));
                    }
                }
            }
        }
        #endregion

        #region 列表绑定
        private void RptBind()
        {
            ddlchannel.SelectedValue = channel;
            txtPsize.Text = psize.ToString();
            txtKeywords.Text = keywords;

            string url = string.Format("http://sou.chinanews.com/search.do?q={0}&ps={1}&channel={2}&sort=score&pager={3}&adv=1", Server.UrlEncode(keywords), psize, channel, page);
            string html = RemoteHelper.GetRemoteHtml(url, Encoding.GetEncoding("utf-8"));

            //截取记录总数
            string countstr = RemoteHelper.GetRegValue("<span style=\"color:red\">(.*?)</span>", html);
            countstr = RemoteHelper.RegDropSides(countstr, "<span style=\"color:red\">", "</span>").Replace(",", "");
            if (int.TryParse(countstr, out rcount))
            {
                string pattern = "<table cellpadding=\"0\" cellspacing=\"0\">([\\w\\W]*?)</table>";
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("title"));
                dt.Columns.Add(new DataColumn("addtime"));
                dt.Columns.Add(new DataColumn("showurl"));
                dt.Columns.Add(new DataColumn("imgurl"));
                dt.Columns.Add(new DataColumn("docollect"));
                foreach (Match m in Regex.Matches(html, pattern))
                {
                    //获取标题
                    string title = RemoteHelper.GetRegValue("<li class=\"news_title(.*?)\"><a (.*?)>(.*?)</a></li>", m.Value);
                    title = RemoteHelper.RegDropSides(title, "<li class=\"news_title(.*?)\"><a (.*?)>", "</a></li>");
                    //获取发布时间
                    string addtime = RemoteHelper.GetRegValue("\\d+?-\\d+?-\\d+? \\d+?:\\d+?:\\d+?", m.Value);
                    //获取链接页面
                    string showurl = RemoteHelper.GetRegValue("<li class=\"news_title(.*?)\"><a (.*?)>(.*?)</a></li>", m.Value);
                    showurl = RemoteHelper.GetHref(showurl);
                    //获取图片链接
                    string imgurl = RemoteHelper.GetRegValue("<td(.*?)valign=\"top\">([\\w\\W]*?)</td>", m.Value);
                    imgurl = RemoteHelper.GetImgSrc(imgurl, "");

                    string docollect = "Y";
                    //不显示视频及图片文章
                    if (showurl.Contains("shipin") || showurl.Contains("/tp/")||showurl.Contains("/bbs."))
                    {
                        docollect = "N";
                    }
                    DataRow dr = dt.NewRow();
                    dr["title"] = title;
                    dr["addtime"] = addtime;
                    dr["showurl"] = showurl;
                    dr["imgurl"] = imgurl;
                    dr["docollect"] = docollect;
                    dt.Rows.Add(dr);
                }
                rptList.DataSource = dt.DefaultView;
                rptList.DataBind();
            }
        }
        #endregion

        #region 组合URL语句
        public string Comb_UrlTxt(int _psize, string _keywords, string _channel)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            strTemp.Append("psize=" + _psize + "&");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("keywords=" + Server.UrlEncode(_keywords) + "&");
            }
            if (!string.IsNullOrEmpty(_channel))
            {
                strTemp.Append("channel=" + _channel + "&");
            }
            else
            {
                strTemp.Append("channel=all&");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 排除的频道
        private string[] ignoreArr = { "/shipin/", "/tp/" };
        #endregion

        //搜索
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("cn_list.aspx?" + Comb_UrlTxt(int.Parse(txtPsize.Text.Trim()), txtKeywords.Text.Trim(), channel) + "page=0");
        }
        //频道
        protected void ddlchannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("cn_list.aspx?" + Comb_UrlTxt(int.Parse(txtPsize.Text.Trim()), keywords, ddlchannel.SelectedValue) + "page=0");
        }
    }
}